﻿using Joker.Model;
using Joker.View.SubForm.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management {
    public partial class PurchaseManage : Form {
        private static string SqlStr = "SELECT [Purchase].[ID], [Purchase].[Date], [Purchase].[State], [Goods].[Name] [GoodsName], [PurchaseDetail].[Amount], [Supply].[Name] [SupplyName] FROM [PurchaseDetail] JOIN [Purchase] ON [Purchase].[ID] = [PurchaseDetail].[PurchaseID] JOIN[Goods] ON [Goods].[ID] = [PurchaseDetail].[GoodsID] JOIN[Supply] ON [Supply].[ID] = [PurchaseDetail].[SupplyID]";
        private static bool Search = true;
        public PurchaseManage() {
            InitializeComponent();
        }

        //窗口加载时 下拉框选择首项，数据表刷新数据
        private void PurchaseEditForm_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        //新增功能
        private void NewData(object sender, EventArgs e) {
            PurchaseLineEdit form = new PurchaseLineEdit();
            OpenForm.NewForm(this, form, false);
            PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        //编辑功能
        private void EditData(object sender, EventArgs e) {
            PurchaseLineEdit form = new PurchaseLineEdit();
            form.PurchaseID = (int)PurchaseEditDataGridView.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, form, false);
            PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        //删除功能
        private void DelData(object sender, EventArgs e) {
            int ID = (int)PurchaseEditDataGridView.CurrentRow.Cells[0].Value;
            //删除前确认
            DialogResult res = MessageBox.Show($"你确定要删除ID为{ID}的所有数据？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (res == DialogResult.OK) {
                //与运算取结果    与运算:一假全假
                bool result = true;
                result &= DBHelper.ExecuteNonQuery($"DELETE FROM [PurchaseDetail] WHERE [PurchaseID] = {ID}");
                result &= DBHelper.ExecuteNonQuery($"DELETE FROM [Purchase] WHERE [ID] = {ID}");
                if (result) {
                    MessageBox.Show("删除成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                } else {
                    MessageBox.Show("删除失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable(SqlStr);
        }

        //查询功能
        private void button1_Click(object sender, EventArgs e) {
            //标志位: 是否为查询状态
            if (Search) {
                button1.Text = "重置";
                Search = false;
                PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable($"{SqlStr} WHERE [Purchase].[Date] BETWEEN CAST(N'{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS Date) AND CAST(N'{dateTimePicker2.Value.ToString("yyyy-MM-dd")}' AS Date) AND [Purchase].[State] = '{(comboBox1.Text.Equals("已通过审核") ? "true" : "false")}'");
            } else {
                button1.Text = "查询";
                Search = true;
                PurchaseEditDataGridView.DataSource = DBHelper.GetDataTable(SqlStr);
            }
        }

        //鼠标点击时选中数据表项
        private void PurchaseEditDataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < PurchaseEditDataGridView.Rows.Count) PurchaseEditDataGridView.CurrentCell = PurchaseEditDataGridView.Rows[e.RowIndex].Cells[0];
        }
    }
}
